package hzmg.akkacloud.server.core.service;

import com.hzmg.akkacloud.common.exception.AkkaCloudException;
import hzmg.akkacloud.server.persistence.remote.model.AppInfoDO;
import hzmg.akkacloud.server.persistence.remote.repository.AppInfoRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 应用信息服务
 *
 * @author zbw
 * @since 2022/7/17
 */
@Service
public class AppInfoService {

    @Resource
    private AppInfoRepository appInfoRepository;

    /**
     * 验证应用访问权限，若没有则注册
     *
     * @param appName  应用名称
     * @param password 密码
     * @return 应用ID
     */
    public Long assertApp(String appName, String password) {

        AppInfoDO appInfo = appInfoRepository.findByAppName(appName).orElseGet(() -> {
            AppInfoDO appInfoDO=new AppInfoDO();
            appInfoDO.setAppName(appName);
            appInfoDO.setPassword(password);
            return appInfoRepository.save(appInfoDO);
        });
            return appInfo.getId();
    }

}
